package am.fp
{        
  import fl.controls.Button;
  import fl.controls.CheckBox;
  import fl.controls.Label;
  import fl.controls.List;
  import fl.controls.TextInput;
  import fl.controls.ScrollPolicy;
  import fl.data.DataProvider;
  import fl.events.ComponentEvent;
  import fl.events.ListEvent;

  import flash.display.MovieClip;
  import flash.display.Sprite;
  import flash.events.MouseEvent; 

  import com.yahoo.astra.fl.containers.VBoxPane;
  import com.yahoo.astra.fl.containers.HBoxPane;
  import com.yahoo.astra.layout.modes.HorizontalAlignment;
  
  import am.fp.PageTile;
  import am.fp.FPDispatcher;
  import am.fp.LocaleManager;
  import am.fp.SearchResultRenderer;
  import am.fp.TextSelection;
  import am.fp.events.*;

  /**
   * Search pane. 
   */
  public class SearchContainer extends VBoxPane
  { 
    // constants
    protected static const PADDING: int = 10;
    protected static const GAP: int = 4;
    
    // members
    protected var m_tiSearch: TextInput;
    protected var m_btnSearch: Button;
    protected var m_cbCaseSensitive: CheckBox;
    protected var m_listResults: List;
    protected var m_lblNoResult: Label;

    /**
     * Constructor.
     */
    public function SearchContainer()
    {
      // align on middle-center and set gap between fields
      setStyle("skin", "FormSkin");
      paddingTop = paddingBottom = paddingLeft = paddingRight = PADDING;
      horizontalAlign = HorizontalAlignment.CENTER;
      horizontalGap = verticalGap = GAP;
      horizontalScrollPolicy = ScrollPolicy.OFF;

      // set default width to zero (pane is hidden)
      width = 0;
      height = NaN;
      
      // add search input field and button
      m_tiSearch = new TextInput();
      m_btnSearch = new Button();
      LocaleManager.instance.setString(m_btnSearch, "label", "IDS_SEARCH");

      var controlsContainer: HBoxPane = new HBoxPane([{target: m_tiSearch, percentWidth: 68}, {target: m_btnSearch, percentWidth: 32}]);
      addChild(controlsContainer);
      controlsContainer.height = 22;
      controlsContainer.horizontalGap = PADDING;
      controlsContainer.horizontalScrollPolicy = ScrollPolicy.OFF;
      controlsContainer.addChild(m_tiSearch);
      controlsContainer.addChild(m_btnSearch);
    
      // add case sensitive checkbox
      m_cbCaseSensitive = new CheckBox();
      LocaleManager.instance.setString(m_cbCaseSensitive, "label", "IDS_CASE_SENSITIVE");
      addChild(m_cbCaseSensitive);
      
      // add label "not found" to be displayed when there is no result
      m_lblNoResult = new Label();
      LocaleManager.instance.setString(m_lblNoResult, "text", "IDS_NOT_FOUND");
      m_lblNoResult.height = 0;
      addChild(m_lblNoResult);
      
      // add results list control
      m_listResults = new List();
      addChild(m_listResults);
      m_listResults.visible = false;
      m_listResults.setStyle("cellRenderer", SearchResultRenderer);
      m_listResults.rowHeight = 32;

      configuration = [
        {target: controlsContainer, percentWidth: 98},
        {target: m_cbCaseSensitive, percentWidth: 98},
        {target: m_lblNoResult, percentWidth: 98},
        {target: m_listResults, percentWidth: 98},
        {target: m_listResults, percentWidth: 98, percentHeight: 100}
      ];
      
      // connect listeners
      FPDispatcher.instance.addEventListener(StatusEvent.CHANGE, onChangeStatus, false, 0, true);
      FPDispatcher.instance.addEventListener(SearchEvent.NEW_RESULT, onNewResult, false, 0, true);
      FPDispatcher.instance.addEventListener(SearchEvent.FINISHED, onSearchFinished, false, 0, true);
      FPDispatcher.instance.addEventListener(SidePaneEvent.CHANGE, onSidePaneChange, false, 0, true);
      m_tiSearch.addEventListener(ComponentEvent.ENTER, onStartSearch, false, 0, true);
      m_btnSearch.addEventListener(ComponentEvent.BUTTON_DOWN, onStartSearch, false, 0, true);
      m_listResults.addEventListener(ListEvent.ITEM_CLICK, onResultClick, false, 0, true);
    }

    /**
     * Cleanup when document is unloaded.
     */
    protected function onChangeStatus(e: StatusEvent): void
    {
      // remove previous searches
      if (e.status == StatusEvent.NO_DOC)
      {
        m_listResults.removeAll();
        m_listResults.visible = false;
        m_lblNoResult.height = 0;
      }
    }
    
    /**
     * Give focus to the text input when the search pane is visible.
     */
    protected function onSidePaneChange(e: SidePaneEvent): void
    {
      if (e.paneId == SidePaneEvent.PANE_SEARCH && e.visible)
        m_tiSearch.setFocus();
    }
    
    /**
     * Begin search.
     */
    protected function onStartSearch(e: ComponentEvent): void
    {
      if (m_tiSearch.text != "")
      {
        m_tiSearch.enabled = false;
        m_btnSearch.enabled = false;
        m_cbCaseSensitive.enabled = false;
        m_listResults.removeAll();
        m_listResults.visible = true;
        m_lblNoResult.height = 0;
        FPDispatcher.instance.dispatchEvent(new SearchEvent(SearchEvent.START, m_tiSearch.text, m_cbCaseSensitive.selected));
      }
    }
    
    /**
     * Add a result into the list.
     */
    protected function onNewResult(e: SearchEvent): void
    {
      m_listResults.addItem({label: e.textToFind, data: e.result});
    }
    
    /**
     * Update UI after search ending.
     */
    protected function onSearchFinished(e: SearchEvent): void
    {
      m_tiSearch.enabled = true;
      m_btnSearch.enabled = true;
      m_cbCaseSensitive.enabled = true;
      if (m_listResults.length == 0)
      {
        m_listResults.visible = false;
        m_lblNoResult.height = 20;
      }
    }
    
    /**
     * Process selection into the results list.
     */
    protected function onResultClick(e: ListEvent): void
    {
      var data: Object = e.item.data;
      FPDispatcher.instance.dispatchEvent(new ModeEvent(ModeEvent.CHANGE, ModeEvent.MODE_TEXT));
      FPDispatcher.instance.dispatchEvent(new SearchResultEvent(SearchResultEvent.SELECT, data.pageNum, data.beginIndex, data.endIndex));
    }
  }
}
